Friday, September 4, 2020

STM32F103R6 SPI Interfaces To SN74HC595N Shift Registers

A serial-in-parallel out shift registers allow a micro-controller to expand its digital output pins. The SN74HC595N is a popular shift registers chip with an 8-bit parallel output. It allows us to cascade a number of registers as many as possible.

STM32F103R6 SPI Interfaces To SN74HC595N Shift Registers
Simulating Program

It can be used as 7-Segment driver, dot matrix display driver, character LCD driver, etc. In this example, I will use it to drive LEDs.

STM32F103R6 SPI Interfaces To SN74HC595N Shift Registers
SN74HC595N DIP Chip

STM32F103R6 SPI Interfaces To SN74HC595N Shift Registers
SN74HC595N Pin Out

 

I use SPI1 communication module of the STM32F103R to interface with the shift registers chip.

STM32F103R6 SPI Interfaces To SN74HC595N Shift Registers
SPI1 Connectivity Setting

I select SPI1 with Haft Duplex Mode. I use PA8 as the Data Latch Enable pin.

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file : main.c
  5.   * @brief : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   * opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private variables ---------------------------------------------------------*/
  24. SPI_HandleTypeDef hspi1;
  25.  
  26. /* Private function prototypes -----------------------------------------------*/
  27. void SystemClock_Config(void);
  28. static void MX_GPIO_Init(void);
  29. static void MX_SPI1_Init(void);
  30. /* USER CODE BEGIN PFP */
  31.  
  32. /**
  33.   * @brief The application entry point.
  34.   * @retval int
  35.   */
  36. int main(void)
  37. {
  38. uint8_t txData=0;
  39. uint8_t rxData=0;
  40.  
  41. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  42. HAL_Init();
  43.  
  44. /* Configure the system clock */
  45. SystemClock_Config();
  46.  
  47. /* USER CODE BEGIN SysInit */
  48.  
  49. /* USER CODE END SysInit */
  50.  
  51. /* Initialize all configured peripherals */
  52. MX_GPIO_Init();
  53. MX_SPI1_Init();
  54.  
  55. /* Infinite loop */
  56. /* USER CODE BEGIN WHILE */
  57. while (1)
  58. {
  59.  
  60. if(HAL_GPIO_ReadPin(SW_GPIO_Port,SW_Pin)==0){
  61. HAL_Delay(250);
  62. txData+=1;
  63.  
  64. HAL_SPI_Transmit(&hspi1, &txData, 1, 10);
  65. HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_SET);
  66.  
  67. }
  68. HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_RESET);
  69. if(txData>0xFF) txData=0;
  70.  
  71. }
  72.  
  73. }
  74.  
  75. /**
  76.   * @brief System Clock Configuration
  77.   * @retval None
  78.   */
  79. void SystemClock_Config(void)
  80. {
  81. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  82. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  83.  
  84. /** Initializes the RCC Oscillators according to the specified parameters
  85.   * in the RCC_OscInitTypeDef structure.
  86.   */
  87. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  88. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  89. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  90. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  91. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  92. {
  93. Error_Handler();
  94. }
  95. /** Initializes the CPU, AHB and APB buses clocks
  96.   */
  97. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  98. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  99. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  100. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  101. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  102. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  103.  
  104. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  105. {
  106. Error_Handler();
  107. }
  108. }
  109.  
  110. /**
  111.   * @brief SPI1 Initialization Function
  112.   * @param None
  113.   * @retval None
  114.   */
  115. static void MX_SPI1_Init(void)
  116. {
  117.  
  118. /* USER CODE END SPI1_Init 1 */
  119. /* SPI1 parameter configuration*/
  120. hspi1.Instance = SPI1;
  121. hspi1.Init.Mode = SPI_MODE_MASTER;
  122. hspi1.Init.Direction = SPI_DIRECTION_1LINE;
  123. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  124. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  125. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  126. hspi1.Init.NSS = SPI_NSS_SOFT;
  127. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  128. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  129. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  130. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  131. hspi1.Init.CRCPolynomial = 10;
  132. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  133. {
  134. Error_Handler();
  135. }
  136. /* USER CODE BEGIN SPI1_Init 2 */
  137.  
  138. /* USER CODE END SPI1_Init 2 */
  139.  
  140. }
  141.  
  142. /**
  143.   * @brief GPIO Initialization Function
  144.   * @param None
  145.   * @retval None
  146.   */
  147. static void MX_GPIO_Init(void)
  148. {
  149. GPIO_InitTypeDef GPIO_InitStruct = {0};
  150.  
  151. /* GPIO Ports Clock Enable */
  152. __HAL_RCC_GPIOA_CLK_ENABLE();
  153.  
  154. /*Configure GPIO pin Output Level */
  155. HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET);
  156.  
  157. /*Configure GPIO pin : EN_Pin */
  158. GPIO_InitStruct.Pin = EN_Pin;
  159. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  160. GPIO_InitStruct.Pull = GPIO_NOPULL;
  161. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  162. HAL_GPIO_Init(EN_GPIO_Port, &GPIO_InitStruct);
  163.  
  164. /*Configure GPIO pin : SW_Pin */
  165. GPIO_InitStruct.Pin = SW_Pin;
  166. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  167. GPIO_InitStruct.Pull = GPIO_PULLUP;
  168. HAL_GPIO_Init(SW_GPIO_Port, &GPIO_InitStruct);
  169.  
  170. }
  171.  
  172. /* USER CODE BEGIN 4 */
  173.  
  174. /* USER CODE END 4 */
  175.  
  176. /**
  177.   * @brief This function is executed in case of error occurrence.
  178.   * @retval None
  179.   */
  180. void Error_Handler(void)
  181. {
  182. /* USER CODE BEGIN Error_Handler_Debug */
  183. /* User can add his own implementation to report the HAL error return state */
  184. __disable_irq();
  185. while (1)
  186. {
  187. }
  188. /* USER CODE END Error_Handler_Debug */
  189. }
  190.  
  191. #ifdef USE_FULL_ASSERT
  192. /**
  193.   * @brief Reports the name of the source file and the source line number
  194.   * where the assert_param error has occurred.
  195.   * @param file: pointer to the source file name
  196.   * @param line: assert_param error line source number
  197.   * @retval None
  198.   */
  199. void assert_failed(uint8_t *file, uint32_t line)
  200. {
  201. /* USER CODE BEGIN 6 */
  202. /* User can add his own implementation to report the file name and line number,
  203.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  204. /* USER CODE END 6 */
  205. }
  206. #endif /* USE_FULL_ASSERT */
  207.  
  208. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  209.  

 

Click here to download its source file.

For other similar posts please check,

  1. Getting Started With STM32F103C8T6 Module with STM32CubeIDE
  2.  STM32F103C8T6 Blue Pill SysTick and Multiplexing Display Example
  3.  STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
  4.  STM32F103C8T6 Blue Pill SysTick LED Blinking
  5. STM32F103R6 Common Anode Seven Segments Display Example 
  6. STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing 
  7. STM32F103R6 Simple 2-Digit Multiplexing Display And Switch Example 
  8. STM32F103R6 SysTick And Digital Clock Example 
  9. STM32F103R6 SysTick Two-Digit Multiplexing Display and Push Button
  10. LED Blinking With STM32F103R6 Using SysTick 
  11. STM32F103R6 SPI Interfaces To SN74HC595N Shift Registers 
  12. STM32F103R6 GPIO Interfaces To A Character LCD In 8-Bit Mode

No comments:

Post a Comment